//
//  NextViewController.m
//  leo
//
//  Created by 常艳 on 15/9/14.
//  Copyright (c) 2015年 mdx. All rights reserved.
//

#import "NextViewController.h"

@interface NextViewController ()

@end

@implementation NextViewController

-(void)dealloc
{
    self.phoneNum=NULL;
    self.syncer=NULL;
}
- (void)viewDidLoad {
   
    [super viewDidLoad];
    self.extendedLayoutIncludesOpaqueBars=YES;
    self.title=@"忘记密码";
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Back_icon@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
    [self.navigationItem setLeftBarButtonItem:leftButton];
    
    float x=self.pass.frame.size.height;
    UIView *passleftView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, x)];
    self.pass.leftView=passleftView;
    self.pass.leftViewMode=UITextFieldViewModeAlways;
    UIView *repassleftView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 20,x)];
    self.repass.leftView=repassleftView;
    self.repass.leftViewMode=UITextFieldViewModeAlways;
    self.syncer=[[SyncManager alloc] init];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    if(self.view.window==nil)
    {
        self.view=nil;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


- (IBAction)register:(id)sender {
    [self.pass resignFirstResponder];
    [self.repass resignFirstResponder];
    [self.indicator startAnimating];
    self.confirmBtn.enabled=NO;
    NSString *pass=[[self.pass text] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSString *repass=[[self.repass text] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if(pass==NULL||[pass length]==0)
    {
        NSLog(@"%@,register, pass or repass is null.",next_tag);
        [self showErrorMsg:@"密码不能为空"];
        return;
    }
    if([pass length]<6)
    {
        [self showErrorMsg:@"密码长度不能少于6个字符"];
        return;
    }
    if(![pass isEqualToString:repass])
    {
        NSLog(@"%@,register, pass is %@ and  repass is %@.",next_tag,pass,repass);
        [self showErrorMsg:@"两次输入的密码不匹配"];
        return;
    }
    if(self.phoneNum==NULL||[self.phoneNum length]==0)
    {
        NSLog(@"%@,register, phoneNUm is null.",next_tag);
        [self showErrorMsg:@"发生错误，请稍后重试"];
        return;
    }
    [self.syncer resetPwd:self.phoneNum password:pass smsCode:@"123456" completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
        [self.indicator stopAnimating];
        self.confirmBtn.enabled=YES;
        if(result==SUCESS)
        {
            [self.navigationController popToRootViewControllerAnimated:YES];
            [LeoMsg showMsg:@"密码修改成功，请登录"];
        }
        else
        {
            NSLog(@"%@,register, sync fail.",next_tag);
            [Tooler showHttpMsg:result description:description msg:@"重置密码失败"];

        }
        
    }];

}
-(void)backAction
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (IBAction)backgroundTaped:(id)sender {
    [self.pass resignFirstResponder];
    [self.repass resignFirstResponder];
}
-(void)showErrorMsg:(NSString *)msg
{
    [self.indicator stopAnimating];
    self.confirmBtn.enabled=YES;
    [LeoMsg showMsg:msg];
}
- (IBAction)passTextFieldChange:(id)sender {
    if(self.pass.text.length>20)
        self.pass.text=[self.pass.text substringToIndex:20];
}
- (IBAction)repassTextFieldChange:(id)sender {
    if(self.repass.text.length>20)
        self.repass.text=[self.repass.text substringToIndex:20];
}

@end
